﻿Public Class Frm_Archive
    Public avo As Dossier
    Dim cmd As SqlClient.SqlCommand
    Public dre As SqlClient.SqlDataReader

    Public Sub Remplir()
        Dim idclt As Integer = 0
        Dim idAdv As Integer = 0
        Dim strclt As String = ""
        Dim strAdv As String = ""
        If Comb_marji3ona.Text <> "جميع الملفات" Then
            Partager.close()
            Try
                Partager.deClt = DirectCast(Comb_marji3ona.SelectedItem, DictionaryEntry)
                Partager.moi.remplir_List_info_fichier()
                List_tous.Items.Clear()
                For Each khib As Dossier In Partager.moi.List_info_fichier
                    If khib.Archive = True And khib.id = CInt(Partager.deClt.Key) Then
                        '--------------------------------------------------------------
                        idAdv = 0
                        strAdv = ""
                        cmd = New SqlClient.SqlCommand("select idAdv from refAdv" & _
                                   " where idRef =" & khib.Ref, Partager.con)
                        Partager.open()
                        dre = cmd.ExecuteReader
                        If dre.HasRows Then
                            dre.Read()
                            idAdv = dre("idAdv")
                        End If
                        dre.Close()
                        Partager.close()

                        For Each enm As Enemie In Partager.moi.List_enemi
                            If idAdv = enm.Id Then
                                If enm.Type = "ذاتي" Then
                                    strAdv = enm.Nom & "  " & enm.Prenom
                                ElseIf enm.Type = "معنوي" Then
                                    strAdv = enm.Nom_ent
                                End If
                                Exit For
                            End If
                        Next

                        '--------------------------------------------------------------
                        idclt = 0
                        strclt = ""
                        cmd = New SqlClient.SqlCommand("select idClt from refClt" & _
                                   " where idRef =" & khib.Ref, Partager.con)
                        Partager.open()
                        dre = cmd.ExecuteReader
                        If dre.HasRows Then
                            dre.Read()
                            idclt = dre("idClt")
                        End If
                        dre.Close()
                        Partager.close()

                        For Each clt As Client In Partager.moi.List_client
                            If idclt = clt.Id Then
                                If clt.Type = "ذاتي" Then
                                    strclt = clt.Nom & "  " & clt.Prenom
                                ElseIf clt.Type = "معنوي" Then
                                    strclt = clt.Nom_ent
                                End If
                                Exit For
                            End If
                        Next
                        '----------------------------------------------------
                        List_tous.Items.Add(New ListViewItem(New String() {khib.id, khib.Num.ToString, khib.Tribunal, khib.Objet, khib.Grade, strclt, strAdv, khib.Etat_appel, khib.Type_appel}))
                    End If
                Next
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        ElseIf Comb_marji3ona.Text = "جميع الملفات" Then
            Try
                Partager.moi.remplir_List_info_fichier()
                List_tous.Items.Clear()
                For Each khib As Dossier In Partager.moi.List_info_fichier
                    If khib.Archive = True Then
                        '--------------------------------------------------------------
                        idAdv = 0
                        strAdv = ""
                        cmd = New SqlClient.SqlCommand("select idAdv from refAdv" & _
                                   " where idRef =" & khib.Ref, Partager.con)
                        Partager.open()
                        dre = cmd.ExecuteReader
                        If dre.HasRows Then
                            dre.Read()
                            idAdv = dre("idAdv")
                        End If
                        dre.Close()
                        Partager.close()

                        For Each enm As Enemie In Partager.moi.List_enemi
                            If idAdv = enm.Id Then
                                If enm.Type = "ذاتي" Then
                                    strAdv = enm.Nom & "  " & enm.Prenom
                                ElseIf enm.Type = "معنوي" Then
                                    strAdv = enm.Nom_ent
                                End If
                                Exit For
                            End If
                        Next

                        '--------------------------------------------------------------
                        idclt = 0
                        strclt = ""
                        cmd = New SqlClient.SqlCommand("select idClt from refClt" & _
                                   " where idRef =" & khib.Ref, Partager.con)
                        Partager.open()
                        dre = cmd.ExecuteReader
                        If dre.HasRows Then
                            dre.Read()
                            idclt = dre("idClt")
                        End If
                        dre.Close()
                        Partager.close()

                        For Each clt As Client In Partager.moi.List_client
                            If idclt = clt.Id Then
                                If clt.Type = "ذاتي" Then
                                    strclt = clt.Nom & "  " & clt.Prenom
                                ElseIf clt.Type = "معنوي" Then
                                    strclt = clt.Nom_ent
                                End If
                                Exit For
                            End If
                        Next
                        '----------------------------------------------------
                        List_tous.Items.Add(New ListViewItem(New String() {khib.id, khib.Num.ToString, khib.Tribunal, khib.Objet, khib.Grade, strclt, strAdv, khib.Etat_appel, khib.Type_appel}))
                    End If
                Next
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End If
    End Sub

    Private Sub Frm_Archive_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Partager.close()
        Partager.archiv = True
        Partager.remplirDossier(Comb_marji3ona, True)
        Comb_marji3ona.SelectedIndex = 0
        '----------------------------------------------------------------------

        '----------------------------------------------------------------------

        '---------------------------------------------------------------------
        Partager.moi.remplir_List_client()
        Partager.moi.remplir_List_enemi()
        Remplir()

    End Sub

    Private Sub Btn_supp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_supp.Click
        If List_tous.SelectedIndices.Count > 0 Then
            If MsgBox("هل تريد فعلا إخراج هذا الملف من الأرشيف إلى الملفات المتداولة ؟", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "أرشفة ملف") = MsgBoxResult.Yes Then
                Try
                    Partager.close()
                    avo.Archive = False
                    Partager.moi.modifier_archive(avo, "0")
                    Partager.archiv = True
                    Partager.remplirDossier(Comb_marji3ona, True)
                    Comb_marji3ona.SelectedIndex = 0

                    Remplir()
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try
            End If
        Else
            MsgBox("اختر ملف من القائمة", MsgBoxStyle.Information, "خطأ")
        End If
    End Sub

    Private Sub List_tous_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles List_tous.SelectedIndexChanged
        If List_tous.SelectedItems.Count > 0 Then
            Try
                Partager.close()
                Dim i As Integer = 0
                Dim id As Integer = CType(List_tous.SelectedItems(0).Text, Integer)
                Dim j As Integer = 0
                While i < List_tous.SelectedIndices.Count

                    avo = Partager.moi.List_info_fichier(j)
                    If avo.id = id Then
                        i += 1
                    End If
                    j += 1

                End While
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End If
    End Sub

    Private Sub Comb_marji3ona_SelectedIndexChanged_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Comb_marji3ona.SelectedIndexChanged
        Remplir()
    End Sub

    Private Sub Btn_Rech_Doss_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_Rech_Doss.Click
        Me.Enabled = False
        Partager.frmafect = Me
        Frm_Recherche.Show()
        Partager.type = Me.Name
        Partager.ar = True
    End Sub

  
End Class